package com.sky.service.impl;


import com.sky.constant.MessageConstant;
import com.sky.constant.DefaultConstant;
import com.sky.context.BaseContext;
import com.sky.dto.GUserInfoDTO;
import com.sky.entity.GUser;
import com.sky.exception.AccountAlradyExists;
import com.sky.exception.AccountNotFoundException;
import com.sky.exception.BaseException;
import com.sky.exception.EmailException;
import com.sky.exception.PasswordErrorException;
import com.sky.mapper.GUserMapper;
import com.sky.service.GUserService;


import com.sky.utils.MailUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

@Service
@Slf4j
public class GUserServiceImpl implements GUserService {

  @Autowired
  private GUserMapper gUserMapper;

  @Override
  public GUser selectUser(GUser gUser) {
    // 这里我知道要提示 用户不存在或者密码错误   但是为了调试方便，我还是分开写
    String originPassword=gUser.getGuserPassword();
    GUser gUserResult = gUserMapper.selectUserByEmail(gUser.getGuserEmail());

    if(gUserResult == null){
      //账号不存在
      throw new AccountNotFoundException(MessageConstant.GUSER_NOT_FOUND);
    }else if(gUserResult.getGuserPassword()==null){
      // 能查到，但是数据库里没有密码
      throw new BaseException(MessageConstant.WRONG_USER_DATA);
    }
    else if( originPassword != null
        && !gUserResult.getGuserPassword().equals(DigestUtils.md5DigestAsHex(originPassword.getBytes())))
    {
      //密码错误，未输入密码的时候，不会进行此判断
      throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
    }
    return gUserResult;
  }

  @Transactional
  @Override
  public void insertUser(GUser gUser) {
    // 判断原来的邮箱是否被注册
    GUser gUserResult = gUserMapper.selectUserByEmail(gUser.getGuserEmail());
    if(gUserResult != null){
      throw new AccountAlradyExists(MessageConstant.GUSER_ALREADY_EXISTS);
    }
    // 设置默认密码
    if(gUser.getGuserPassword()==null){
      gUser.setGuserPassword(DefaultConstant.DEFAULT_PASSWORD);
    }
    // 对guser密码进行md5加密
    String newPassword=DigestUtils.md5DigestAsHex(gUser.getGuserPassword().getBytes());
    gUser.setGuserPassword(newPassword);
    gUserMapper.insertGUser(gUser);
  }


  @Override
  @Transactional
  public void updateUserByEmail(GUser gUser) {
    // 判断是否存在用户
    GUser gUserResult = gUserMapper.selectUserByEmail(gUser.getGuserEmail());
    if(gUserResult == null){
      throw new AccountAlradyExists(MessageConstant.GUSER_NOT_FOUND);
    }
    gUser.setGuserId(gUserResult.getGuserId());
    if(gUser.getGuserPassword()!=null){
      // 密码md5加密
      String newPassword=DigestUtils.md5DigestAsHex(gUser.getGuserPassword().getBytes());
      gUser.setGuserPassword(newPassword);
    }
    if(gUser.getGuserEmail()!=null){
      if(!MailUtils.checkMailVaild(gUser.getGuserEmail())){
        // 邮箱格式不对
        throw new EmailException(MessageConstant.EMAIL_NOT_VAILD);
      }
    }
    if(!DefaultConstant.DEFAULT_GENDER.contains(gUser.getGuserSex())){
      throw new BaseException(MessageConstant.WRONG_GENDER_INDEX);
    }

    gUserMapper.updateUser(gUser);
  }

  @Override
  public Boolean verfiyEmail(String email, String verifycode) {
    String actualCode =  MailUtils.expiringMap.get(email);
    if( actualCode != null && actualCode.equals(verifycode)){
      return true;
    }else{
      throw new EmailException(MessageConstant.EMAIL_VERFIY_CODE_ERROR);
    }
  }

  @Override
  public GUser selectUserById() {
    int gUserId = BaseContext.getCurrentId();
    GUser gUser=gUserMapper.selectUserById(gUserId);
    return gUser;
  }

  @Override
  public void updateUserById(GUser gUser) {
    if(gUser.getGuserPassword()!=null){
      // 密码md5加密
      String newPassword=DigestUtils.md5DigestAsHex(gUser.getGuserPassword().getBytes());
      gUser.setGuserPassword(newPassword);
    }
    if(gUser.getGuserEmail()!=null){
      if(!MailUtils.checkMailVaild(gUser.getGuserEmail())){
        // 邮箱格式不对
        throw new EmailException(MessageConstant.EMAIL_NOT_VAILD);
      }
    }
    if(!DefaultConstant.DEFAULT_GENDER.contains(gUser.getGuserSex())){
      throw new BaseException(MessageConstant.WRONG_GENDER_INDEX);
    }
    gUserMapper.updateUser(gUser);
  }

  @Override
  public void updateEmail(String oldEmail, String newEmail) {
    // 用户更新email
    GUser gUser=selectUserById();
    // 老邮箱验证
    if(gUser.getGuserEmail() ==null){
      throw new BaseException(MessageConstant.EMAIL_NOT_SET);
    }else if(!gUser.getGuserEmail().equals(oldEmail)){
      throw new EmailException(MessageConstant.EMAIL_NOT_SAME);
    }
    // 新邮箱验证
    GUser gUserNewEmail=gUserMapper.selectUserByEmail(newEmail);
    if(gUserNewEmail!=null){
      throw new AccountAlradyExists(MessageConstant.GUSER_ALREADY_EXISTS);
    }

    // 同意更换
    gUser.setGuserEmail(newEmail);
    gUserMapper.updateUser(gUser);
  }
}
